package com.example.demo.web;

import com.example.demo.domain.entity.ParameterDefinitionEntity;
import com.example.demo.domain.entity.ParameterEntity;
import com.example.demo.service.ParameterFrontService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/home")
@Slf4j
public class RouteController {
    @Autowired
    ParameterFrontService parameterFrontService;

    @GetMapping("/oauthtest")
    public String getOauthtest() {
        return "oauth";
    }

    @GetMapping("/p")
    public String getP() {
        return "parameter2";
    }

    @GetMapping("/p/edit")
    public String addP() {
        return "parameterEdit";
    }

    @GetMapping("/p/{pid}/edit")
    public String editP(@PathVariable Long pid, Model model) {
        ParameterEntity singleP = parameterFrontService.getSingleP(pid);
        model.addAttribute("p", singleP);
        return "parameterEdit";
    }
    /***********************************************************PD****************************************************/
    @GetMapping("/p/{pid}/pd")
    public String getpd(@PathVariable String pid, Model model) {
        model.addAttribute("pid", pid);
        return "parameterDefinition";
    }
    @GetMapping("/p/{pid}/pd/edit")
    public String addPD(@PathVariable String pid, Model model) {
        model.addAttribute("pid", pid);
        return "parameterDefinitionEdit";
    }

    @GetMapping("/p/{pid}/pd/{pdid}/edit")
    public String editPD(@PathVariable String pid, @PathVariable Long pdid, Model model) {
        model.addAttribute("pid", pid);
        ParameterDefinitionEntity singlePd = parameterFrontService.getSinglePd(pdid);
        model.addAttribute("pd", singlePd);
        return "parameterDefinitionEdit";
    }

    /***********************************************************PV****************************************************/

    @GetMapping("/p/{pid}/view")
    public String getPv(@PathVariable Long pid, Model model) {
        model.addAttribute("pid", pid);
        return "parameterView";
    }
    @GetMapping("/p/{pid}/v")
    public String getPv0(@PathVariable Long pid, Model model) {
        model.addAttribute("pid", pid);
        return "parameterValue";
    }
    @GetMapping("/p/{pid}/v/edit")
    public String addPv(@PathVariable Long pid, Model model) {
        model.addAttribute("pid", pid);
        List<ParameterDefinitionEntity> pd = parameterFrontService.getPD(pid);
        model.addAttribute("pd", pd);
        return "parameterValueEdit";
    }
    @GetMapping("/p/{pid}/v/{pvid}/edit")
    public String editPv(@PathVariable Long pid, @PathVariable Long pvid, Model model) {
        model.addAttribute("pid", pid);
        Map<String, String> pv = parameterFrontService.getPvById(pvid);
        model.addAttribute("pv", pv);
        //pv.forEach((k,v)-> log.info(k+"---"+v));
        return "parameterValueEdit";
    }
}
